const multer = require("@koa/multer");
const { UPLOAD_PATH } = require("./constant");

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, UPLOAD_PATH);
  },
  filename: function (req, file, cb) {
    // 解决中文乱码问题
    file.originalname = Buffer.from(file.originalname, "latin1").toString(
      "utf8"
    );
    // 找到文件后缀的分割点
    let dotIndex = file.originalname.lastIndexOf(".");
    console.log(dotIndex);
    let fileName = file.originalname.slice(0, dotIndex);
    let suffix = file.originalname.slice(dotIndex);
    console.log("17", fileName, suffix);
    cb(null, "file_" + new Date().getTime() + "_" + fileName + suffix);
  },
});
module.exports = storage;
